package com.lvtongka.app.ui.qr

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.lvtongka.app.base.BaseViewModel
import com.lvtongka.app.common.bean.AccountRequest
import com.lvtongka.app.common.bean.RealNameRequest
import com.lvtongka.app.fragment.common.bean.SetPayPwdRequest
import com.lvtongka.app.fragment.home.bean.TravelPassAssetsData
import com.lvtongka.app.fragment.home.bean.TravelPassAssetsResponse
import com.lvtongka.app.fragment.my.bean.MyInfo
import com.lvtongka.app.fragment.my.bean.MyResponse
import com.lvtongka.app.fragment.pay.bean.OpenScanPayRequest
import com.lvtongka.app.fragment.pay.bean.PayStatus
import com.lvtongka.app.fragment.pay.bean.PayStatusRequest
import com.lvtongka.app.fragment.pay.bean.PayStatusResponse
import com.lvtongka.app.fragment.pay.bean.PaymentData
import com.lvtongka.app.fragment.pay.bean.PaymentResponse
import com.lvtongka.app.fragment.pay.bean.ScanDynamicsQrCodeData
import com.lvtongka.app.fragment.pay.bean.ScanDynamicsQrCodeRequest
import com.lvtongka.app.fragment.pay.bean.ScanDynamicsQrCodeResponse
import com.lvtongka.app.fragment.pay.bean.ScanQrCodeData
import com.lvtongka.app.fragment.pay.bean.ScanQrCodeRequest
import com.lvtongka.app.fragment.pay.bean.ScanQrCodeResponse
import com.lvtongka.app.fragment.pay.bean.SetPayTypeRequest
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.HttpCallBack
import com.lvtongka.app.http.OkhttpUtils
import com.lvtongka.app.http.common.UrlConstants
import com.lvtongka.app.ui.coupon.bean.CouponPage
import com.lvtongka.app.ui.coupon.bean.CouponResponse
import com.lvtongka.app.ui.coupon.bean.QrCouponRequest
import com.lvtongka.app.ui.login.bean.SendAt
import com.lvtongka.app.ui.login.bean.SendResponse
import com.lvtongka.app.ui.qr.bean.GenerateQRCode
import com.lvtongka.app.ui.qr.bean.GenerateQRCodeRequest
import com.lvtongka.app.ui.qr.bean.GenerateQRCodeResponse
import com.lvtongka.app.ui.qr.bean.PaymentApp
import com.lvtongka.app.ui.qr.bean.PaymentAppCancelRequest
import com.lvtongka.app.ui.qr.bean.PaymentAppRequest
import com.lvtongka.app.ui.qr.bean.PaymentAppResponse
import com.lvtongka.app.ui.qr.bean.PaymentCouponRequest
import com.lvtongka.app.ui.qr.bean.PaymentMerchantRequest
import com.lvtongka.app.ui.qr.bean.SendCodeNotCellRequest

class PaymentCodeViewModel : BaseViewModel() {
    private val _realName = MutableLiveData<Boolean>()
    val realName:LiveData<Boolean> = _realName

    private val _paymentData = MutableLiveData<PaymentData>()
    val paymentData: LiveData<PaymentData> = _paymentData

    private val _setPwd = MutableLiveData<Boolean>()
    val setPwd:LiveData<Boolean> = _setPwd

    private val _payStatus = MutableLiveData<PayStatus>()
    val payStatus:LiveData<PayStatus> = _payStatus

    private val _payQuanStatus = MutableLiveData<PayStatus>()
    val payQuanStatus:LiveData<PayStatus> = _payQuanStatus

    private val _scanQrCodeData = MutableLiveData<ScanQrCodeData>()
    val scanQrCodeData:LiveData<ScanQrCodeData> = _scanQrCodeData

    private val _scanDynamicsQrCodeData = MutableLiveData<ScanDynamicsQrCodeData>()
    val scanDynamicsQrCodeData:LiveData<ScanDynamicsQrCodeData> = _scanDynamicsQrCodeData

    private val _couponPage= MutableLiveData<CouponPage>()
    val couponPage: LiveData<CouponPage> = _couponPage

    private val _myInfo = MutableLiveData<MyInfo>()
    val myInfo: LiveData<MyInfo> = _myInfo

    private val _openScanPay = MutableLiveData<Boolean>()
    val openScanPay: LiveData<Boolean> = _openScanPay

    private val _setPayType = MutableLiveData<Boolean>()
    val setPayType: LiveData<Boolean> = _setPayType

    private val _travelPassAssetsData = MutableLiveData<TravelPassAssetsData>()
    val travelPassAssetsData:LiveData<TravelPassAssetsData> = _travelPassAssetsData

    private val _paymentResult = MutableLiveData<Boolean>()
    val paymentResult: LiveData<Boolean> = _paymentResult

    private val _cancelResult = MutableLiveData<Boolean>()
    val cancelResult: LiveData<Boolean> = _cancelResult

    private val _sendAt= MutableLiveData<SendAt>()
    val sendAt: LiveData<SendAt> = _sendAt

    private val _paymentResultDt = MutableLiveData<PaymentApp>()
    val paymentResultDt: LiveData<PaymentApp> = _paymentResultDt

    private val _qrCodeData = MutableLiveData<GenerateQRCode>()
    val qrCodeData: LiveData<GenerateQRCode> = _qrCodeData

    /**
     * 发送短信验证码
     */
    fun sendNotCellSmsCode() {
        val request = SendCodeNotCellRequest()
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, SendResponse::class.java)
                    _sendAt.postValue(resp.data)
                }
            }, getAccessToken())
    }

    // 生成付款码
    fun getQrCode() {
        val request = AccountRequest("21")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val payment = Gson().fromJson(result, PaymentResponse::class.java)
                    _paymentData.postValue(payment.data)
                }
            }, getAccessToken())
    }

    /**
     * 实名认证
     */
    fun realNameAuthentication(name: String, idNo: String) {
        val  request = RealNameRequest(name, idNo)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, true)
                    _realName.postValue(true)
                }
            }, getAccessToken())
    }

    /**
     * 设置密码
     */
    fun setPayPassword(pwd: String) {
        val  request = SetPayPwdRequest(pwd)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    SPHelper.putBoolean(SPKConstants.IS_SET_PWD, true)
                    _setPwd.postValue(true)
                }
            }, getAccessToken())
    }

    // 查询付款状态
    fun queryPayStatus(qrCode: String) {
        val request = PayStatusRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                    getQrCode()
                }

                override fun onSuccess(result: String) {
                    val payStatus = Gson().fromJson(result, PayStatusResponse::class.java)
                    _payStatus.postValue(payStatus.data)
                }
            }, getAccessToken())
    }

    // 查询券付款状态
    fun queryPayQuanStatus(qrCode: String) {
        val request = PayStatusRequest(qrCode, "41")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT_COUPON, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error2.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val payStatus = Gson().fromJson(result, PayStatusResponse::class.java)
                    _payQuanStatus.postValue(payStatus.data)
                }
            }, getAccessToken())
    }

    // 识别商户静态二维码
    fun scanQrCode(qrCode: String) {
        val request = ScanQrCodeRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val scanQrCodeResponse = Gson().fromJson(result, ScanQrCodeResponse::class.java)
                    _scanQrCodeData.postValue(scanQrCodeResponse.data)
                }
            }, getAccessToken())
    }

    // 识别商户动态二维码
    fun scanDynamicsQrCode(qrCode: String) {
        val request = ScanDynamicsQrCodeRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val scanQrCodeResponse = Gson().fromJson(result, ScanDynamicsQrCodeResponse::class.java)
                    _scanDynamicsQrCodeData.postValue(scanQrCodeResponse.data)
                }
            }, getAccessToken())
    }

    // 查询优惠券列表
    fun getCouponList(qrCode: String?) {
        val request = QrCouponRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_COUPON_PACKAGE, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, CouponResponse::class.java)
                    _couponPage.postValue(resp.data.page)
                }
            }, getAccessToken())
    }

    fun getMyInfo() {
        val request = AccountRequest("03")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    // 存储数据
                    SPHelper.putString(SPKConstants.BASIC_INFO, result)
                    val my = Gson().fromJson(result, MyResponse::class.java)
                    when(my.data.cell_bind_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, true)
                    }
                    when(my.data.real_name_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, true)
                    }
                    when(my.data.password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, true)
                    }
                    when(my.data.func_scan_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, true)
                    }
                    when(my.data.no_password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, true)
                    }
                    SPHelper.putInt(SPKConstants.MY_LIMIT_NO_PASSWORD, my.data.limit_no_password)
                    SPHelper.putString(SPKConstants.MY_CELL, my.data.cell)
                    SPHelper.putString(SPKConstants.MEMBER_ID, my.data.member_id)
                    _myInfo.postValue(my.data)
                }
            }, getAccessToken())
    }

    // 开通扫码支付
    fun openScanPay(pwd: String) {
        val request = OpenScanPayRequest(pwd)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _openScanPay.postValue(true)
                }
            }, getAccessToken())
    }

    // 设置付款码扣款方式
    fun setPayType(payType: String?) {
        val request = SetPayTypeRequest(payType)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _setPayType.postValue(true)
                }
            }, getAccessToken())
    }

    // 查询资产
    fun queryAssets() {
        val request = AccountRequest("65")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, TravelPassAssetsResponse::class.java)
                    _travelPassAssetsData.postValue(resp.data)
                }
            }, getAccessToken())
    }

    /**
     *  静态商户二维码用自己或者亲属付款
     */
    fun paymentMerchant(request : PaymentMerchantRequest) {
        val json = Gson().toJson(request)
        OkhttpUtils.getInstance()
            .postRequest(json, UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _paymentResult.postValue(true)
                }
            }, getAccessToken())
    }

    /**
     *  静态商户二维码用券付款
     */
    fun paymentMerchantCoupon(request : PaymentCouponRequest) {
        val json = Gson().toJson(request)
        OkhttpUtils.getInstance()
            .postRequest(json, UrlConstants.API_ACCOUNT_COUPON, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _paymentResult.postValue(true)
                }
            }, getAccessToken())
    }

    /**
     *  动态商户二维码付款
     */
    fun paymentApp(request : PaymentAppRequest) {
        val json = Gson().toJson(request)
        OkhttpUtils.getInstance()
            .postRequest(json, UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, PaymentAppResponse::class.java)
                    _paymentResultDt.postValue(resp.data)
                }
            }, getAccessToken())
    }

    /**
     * 动态商户二维码付款取消
     */
    fun cancelPaymentApp(request : PaymentAppCancelRequest) {
        val json = Gson().toJson(request)
        OkhttpUtils.getInstance()
            .postRequest(json, UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _cancelResult.postValue(true)
                }
            }, getAccessToken())
    }

    fun generateQRCode(qrCode: String) {
        val request = GenerateQRCodeRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT_COUPON, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error2.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val resp = Gson().fromJson(result, GenerateQRCodeResponse::class.java)
                    _qrCodeData.postValue(resp.data)
                }
            }, getAccessToken())

    }
}